//
//  PGFavoriteViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-2-12.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGFavoriteViewController.h"
#import "PGArticleManager.h"
#import "PGFavor.h"
#import "PGArticle.h"
#import "MobClick.h"
#import "PGDetailViewController.h"
@interface PGFavoriteViewController ()

@end

@implementation PGFavoriteViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)init{
    self = [super initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGFavoriteViewController":@"PGFavoriteViewController_iPad" bundle:nil];
    if(self){
        
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _list = [PGArticleManager sharedManager].favors;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"Favors"];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"Favors"];
}
- (void)viewDidUnload {
    [self setTvCell:nil];
    [super viewDidUnload];
}
#pragma mark - uitableview delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _list.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?76:132;
}
//-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return @"深空天体";
//}
//-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *groupHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
//    UIImageView *headerBgImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"header_bg.jpg"]];
//    [groupHeaderView addSubview:headerBgImage];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 20)];
//    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
//    label.backgroundColor = [UIColor clearColor];
//    label.textColor = [UIColor colorWithWhite:0.2 alpha:0.6];
//    label.font = [UIFont boldSystemFontOfSize:12];
//    [groupHeaderView addSubview:label];
//    return groupHeaderView;
//}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PGFavoriteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGFavoriteCell"];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGFavoriteCell":@"PGFavoriteCell_iPad" owner:self options:nil];
        cell = _tvCell;
        self.tvCell = nil;
    }
    PGFavor*favor = [_list objectAtIndex:indexPath.row];
    PGArticle *article = [[PGArticleManager sharedManager]articleWithID:favor.articleid.intValue];
    cell.titleLabel.text = article.name;
    cell.viewsLabel.text = [NSString stringWithFormat:@"%d人浏览", article.views.intValue];
    cell.favorsLabel.text = [NSString stringWithFormat:@"%d人喜欢", article.favors.intValue];
    NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:article.time.intValue];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
    cell.dateLabel.text = [NSString stringWithFormat:@"%.4d-%.2d-%.2d",components.year,components.month,components.day];
    [cell loadImage:article.imageSmall];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PGDetailViewController *controller = [[PGDetailViewController alloc]initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGDetailViewController":@"PGDetailViewController_iPad" bundle:nil];
    controller.articleId = [(PGFavor*)[_list objectAtIndex:indexPath.row] articleid].intValue;
    [self.navigationController pushViewController:controller animated:YES];
}
- (IBAction)backHandle:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
